package com.tomato.spring.basic.web.dataImport;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.tomato.spring.basic.web.dataImport.dataBean.EntryDetail;
import com.tomato.spring.basic.web.dataImport.dataBean.EntryTitPet;
import com.tomato.spring.basic.web.dataImport.dataBean.RootDataBean;
import com.tomato.spring.basic.web.logic.bean.PetVarietyDetailInfo;
import com.tomato.spring.basic.web.logic.service.PetVarietyDetailInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: knowledge-pooling
 * @ClassName CutePetImportUtil
 * @description:
 * @author: tomato
 * @create: 2024-07-07 19:48
 * @Version 1.0
 **/
@Configuration
@Slf4j
public class CutePetImportUtil {

    //通过 @Autowired 将service 注入到所在类中
    @Autowired
    private PetVarietyDetailInfoService petVarietyDetailInfoService;

    //定义一个本类名的静态变量
    private static CutePetImportUtil cutePetImportUtil;

    /**
     *     @PostConstruct该注解被用来修饰一个非静态的void（）方法；
     *     被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器执行一次。PostConstruct在构造函数之后执行，init（）方法之前执行
     *     方法执行先后顺序为： Constructor > @Autowired > @PostConstruct
     *     将service 接口名称赋值给静态变量
     */
    @PostConstruct
    public void init() {
        cutePetImportUtil = this;
        cutePetImportUtil.petVarietyDetailInfoService = this.petVarietyDetailInfoService;
    }
    public static void importData() {
        String jsonFilePath = "/Users/fanxiaodiu/Desktop/Project/宠物资料/pets-master/爬虫/data.json";
        log.info("import data from external system");
        List<RootDataBean> yourEntityList = Lists.newArrayList();
        // save data to database
        try {
            // 初始化 ObjectMapper，Jackson 的核心类之一，用于JSON的读和写
            ObjectMapper objectMapper = new ObjectMapper();

            // 从文件中读取JSON字符串并转换为对象实体
            File jsonFile = new File(jsonFilePath);
            //Object yourEntity = objectMapper.readValue(jsonFile, Object.class);
            yourEntityList = objectMapper.readValue(jsonFile, new TypeReference<List<RootDataBean>>(){});

            // 现在 yourEntity 对象包含了从JSON文件解析而来的数据，可以对其进行操作
            System.out.println("转换后的对象实体：" + yourEntityList.size());

        } catch (IOException e) {
            e.printStackTrace();
        }
        List<PetVarietyDetailInfo> petVarietyDetailInfoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(yourEntityList)){
            // 遍历 yourEntityList，将其转换为 PetVarietyDetailInfo 实体并保存到数据库
            for (RootDataBean rootDataBean : yourEntityList) {
                PetVarietyDetailInfo petVarietyDetailInfo = new PetVarietyDetailInfo();
                petVarietyDetailInfo.setPetCategoryId(5);
                petVarietyDetailInfo.setPetVarietyNameCn(rootDataBean.getName());
                if (CollectionUtils.isNotEmpty(rootDataBean.getEntry_tit_pet())){
                    for (EntryTitPet entry_tit_pet : rootDataBean.getEntry_tit_pet()) {
                        if (StringUtils.isBlank(entry_tit_pet.getKey())||StringUtils.isBlank(entry_tit_pet.getValue())){
                            continue;
                        }
                        switch (entry_tit_pet.getKey()){
                            case "英文名":
                                petVarietyDetailInfo.setPetVarietyNameEn(entry_tit_pet.getValue());
                                break;
                            case "性格":
                                petVarietyDetailInfo.setPetVarietyCharacter(entry_tit_pet.getValue());
                                break;
                            case "祖籍":
                                petVarietyDetailInfo.setPetVarietyDescent(entry_tit_pet.getValue());
                                break;
                            case "易患病":
                                petVarietyDetailInfo.setPetVarietySicken(entry_tit_pet.getValue());
                                break;
                            case "寿命":
                                petVarietyDetailInfo.setPetVarietyLife(entry_tit_pet.getValue());
                                break;
                            case "价格":
                                petVarietyDetailInfo.setPetVarietyPrice(entry_tit_pet.getValue());
                                break;
                            default:
                                break;
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(rootDataBean.getEntry_detail())){
                    for (EntryDetail entryDetail : rootDataBean.getEntry_detail()) {
                        if (StringUtils.isBlank(entryDetail.getKey())||StringUtils.isBlank(entryDetail.getValue())){
                            continue;
                        }
                        switch (entryDetail.getKey()){
                            case "品种简介":
                                petVarietyDetailInfo.setPetVarietyDetail(entryDetail.getValue());
                                break;
                            case "形态特征":
                                petVarietyDetailInfo.setPetVarietyAppearance(entryDetail.getValue());
                                break;
                            case "性格特点":
                                petVarietyDetailInfo.setPetVarietyCharacterDetail(entryDetail.getValue());
                                break;
                            case "养护知识":
                                petVarietyDetailInfo.setPetVarietyConserve(entryDetail.getValue());
                                break;
                            case "喂食要点":
                                petVarietyDetailInfo.setPetVarietyFeedingRequire(entryDetail.getValue());
                                break;
                            default:
                                break;
                        }
                    }
                }
                petVarietyDetailInfoList.add(petVarietyDetailInfo);
                }
        }
        cutePetImportUtil.petVarietyDetailInfoService.saveBatch(petVarietyDetailInfoList);
        log.info("import data from external system finished");
    }
}
